using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.IO;
using System.Configuration;
using TaskBarNotifier;

namespace FeedNotifier.Main
{
	public partial class Form1 : Form
	{
		private Configuration appConf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
		private TaskbarNotifier tbn;
		private StringCollection notifiedEmail = new StringCollection();


		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			this.appConf.Save(ConfigurationSaveMode.Minimal);
			this.timerPoll.Start();
			CheckFeed();
		}

		private void CheckFeed()
		{
			string username = this.tbUsername.Text.Trim();
			string password = this.tbPassword.Text.Trim();
			string domain = this.tbDomain.Text.Trim();

			WebRequest wr = WebRequest.Create(string.Format("https://mail.google.com/hosted/{0}/feed/atom",domain));

			byte[] bytes = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, password));

			wr.Headers.Add("Authorization",
							   "Basic " + Convert.ToBase64String(bytes));

			Stream feedStream = wr.GetResponse().GetResponseStream();

			Atom.Core.AtomFeed myFeed = Atom.Core.AtomFeed.Load(feedStream);

			// Get the date and time of the feed.
			string strFeedTime = myFeed.Modified.DateTime.Day.ToString() + "-"
					 + myFeed.Modified.DateTime.ToString("MMM") + "-"
					 + myFeed.Modified.DateTime.Year.ToString() + " "
					 + myFeed.Modified.DateTime.TimeOfDay.ToString();

			DateTime feedTime = DateTime.Parse(strFeedTime);

			Atom.Core.Collections.AtomEntryCollection ents =
												 myFeed.Entries;

			tbn = new TaskbarNotifier();
			tbn.SetBackgroundBitmap(new Bitmap(GetType(), "skin.bmp"), Color.FromArgb(255, 0, 255));
			tbn.TitleRectangle = new Rectangle(40, 9, 200, 25);
			tbn.ContentRectangle = new Rectangle(8, 41, 250, 68);


			foreach (Atom.Core.AtomEntry ent in ents)
			{
				if (!this.notifiedEmail.Contains(ent.Id.LocalPath))
				{
					string FromName = ent.Author.Name;
					string Title = ent.Title.Content;
					string Content = ent.Summary.Content;

					tbn.Show(FromName, Content, 500, 2000, 500);

					this.notifiedEmail.Add(ent.Id.LocalPath);
				}

			}
		}

		private void timerPoll_Tick(object sender, EventArgs e)
		{
			this.CheckFeed();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			this.CheckConfigKey("username");
			this.CheckConfigKey("password");
			this.CheckConfigKey("domain");
						

			this.tbUsername.Text = this.appConf.AppSettings.Settings["username"].Value;
			this.tbPassword.Text = this.appConf.AppSettings.Settings["password"].Value;
			this.tbDomain.Text = this.appConf.AppSettings.Settings["domain"].Value;
		}

		private void CheckConfigKey(string key)
		{
			if (this.appConf.AppSettings.Settings[key] == null)
				this.appConf.AppSettings.Settings.Add(key, key);
			
			this.appConf.Save();
		}
	}
}